package test2;

public class AntMinStack {

    // auxiliary stack, used to record current min value in whole stack;
    private Stack<Integer> minStack = new Stack<>();
    private Stack<Integer> dataStack = new Stack<>();

    // push an item to top
    public void push(Integer item){
        dataStack.push(item);
        if(minStack.length == 0 || item<=minStack.head.data){
            minStack.push(item);
        }else{
            minStack.push(minStack.head.data);
        }
    }

    // pop an item from top
    public Integer pop(){
        if(dataStack.length == 0 || minStack.length == 0){
            return null;
        }
        minStack.pop();
        return dataStack.pop();
    }

    // return min value in stack,not modify the node list
    public Integer min(){
        if(minStack.length == 0){
            return null;
        }
        return minStack.head.data;
    }

    public static void main(String[] args){
        AntMinStack antMinStack = new AntMinStack();
        antMinStack.push(1);
        antMinStack.push(2);
        antMinStack.push(3);
        System.out.println(antMinStack.pop());
        antMinStack.push(0);
        System.out.println(antMinStack.min());
    }
}